/*
package com.hong.utils;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

*/
/**
 * @Auther: Administrator
 * @Date: 2018/7/21 20:12
 * @Description:
 *//*

public class CountDownLatchExample4 {

    private static Random random = new Random(System.currentTimeMillis());

    static class Event{
        int id=0;

        public Event(int id) {
            this.id = id;
        }
    }

    interface Watcher{

        void done(Table table);
    }

    static class TaskBatch implements Watcher{

        private CountDownLatch countDownLatch;

        public TaskBatch(int size){
            this.countDownLatch  = new CountDownLatch(size);
        }

        public void done(Table table) {
            countDownLatch.countDown();
            if(countDownLatch.getCount()==0){
                System.out.println("The table"+table.tableName+"finished work,["+table+"]");
            }
        }
    }

    static class TaskGroup implements Watcher{

        private CountDownLatch countDownLatch;

        private TaskGroup taskGroup;

        public TaskGroup(TaskGroup taskGroup,int size){
            this.taskGroup = taskGroup;
            this.countDownLatch  = new CountDownLatch(size);
        }

        public void done(Table table) {
            countDownLatch.countDown();
            if(countDownLatch.getCount()==0){
                System.out.println("The table"+table.tableName+"finished work,["+table+"]");
                taskGroup.done(table);
            }
        }
    }

    static class Table{
        String tableName;
        long sourceRecordCount = 10;
        long targetCount;
        String columnSchema = "<table name='a'><column name='coll' type='varchar2' /></table>";
        String targetColumnSchema = "";
        String sourceColumnSchema = "";

        public Table(String tableName, long sourceRecordCount) {
            this.tableName = tableName;
            this.sourceRecordCount = sourceRecordCount;
            this.targetCount = targetCount;
            this.columnSchema = columnSchema;
            this.targetColumnSchema = targetColumnSchema;
        }

        @Override
        public String toString() {
            return "Table{" +
                    "tableName='" + tableName + '\'' +
                    ", sourceRecordCount=" + sourceRecordCount +
                    ", targetCount=" + targetCount +
                    ", columnSchema='" + columnSchema + '\'' +
                    ", targetColumnSchema='" + targetColumnSchema + '\'' +
                    ", sourceColumnSchema='" + sourceColumnSchema + '\'' +
                    '}';
        }
    }

    private static List<Table> capture(Event event){
        List<Table> list  = new ArrayList<Table>();
        for (int i = 0;i<10;i++){
            list.add(new Table("table-"+event.id+" -"+i,i*1000));
        }
        return list;

    }

    public static void main(String[] args) {
        Event[] events ={new Event(1),new Event(2)};
        ExecutorService executorService = Executors.newFixedThreadPool(5);


        for (Event event:events){
            List<Table> tables = capture(event);
            TaskGroup taskGroup  = new TaskGroup(tables.size(),event);
            for (Table table:tables){
                TrustSourceColumns trustSourceColumns = new TrustSourceColumns(table);
                TrustSorceRecordCount trustSorceRecordCount = new TrustSorceRecordCount(table, taskBatch);
                TaskBatch taskBatch = new TaskBatch(2);
                executorService.submit(trustSourceColumns);
                executorService.submit(trustSorceRecordCount);
            }
        }
     }

     static class TrustSorceRecordCount implements Runnable{

        private final Table table;

        private final TaskBatch taskBatch;

         TrustSorceRecordCount(Table table, TaskBatch taskBatch) {
             this.table = table;
             this.taskBatch = taskBatch;
         }


         public void run() {

             try {
                 Thread.sleep(random.nextInt(10000));
             } catch (InterruptedException e) {
                 e.printStackTrace();
             }
             table.targetCount = table.sourceRecordCount;
             taskBatch.done(table);

         }
     }

    static class TrustSourceColumns implements Runnable{

        private final Table table;

        TrustSourceColumns(Table table) {
            this.table = table;
        }

        public void run() {
            try {
                Thread.sleep(random.nextInt(10000));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            table.targetColumnSchema = table.sourceColumnSchema;
            System.out.println("The table "+table.tableName+" target colums capture done and update the data");


        }
    }

}
*/
